from rest_framework.views import APIView
from rest_framework.response import Response
from .models import CameraBrand, Camera
from .serializers import CameraBrandSerializer, CameraListSerializer,CameraDetailSerializer
from rest_framework.generics import RetrieveAPIView

class CameraBrandListView(APIView):
    def get(self, request):
        brands = CameraBrand.objects.all()
        serializer = CameraBrandSerializer(brands, many=True)
        return Response(serializer.data)


#从后端发送出相机卡片的内容

class CameraListView(APIView):
    def get(self, request):
        brand_id = request.query_params.get('brand')
        if not brand_id:

            return Response([])

        cameras_name = CameraBrand.objects.filter(id=brand_id)
        print(cameras_name)
        cameras = Camera.objects.filter(brand=cameras_name[0].name)

        serializer = CameraListSerializer(cameras, many=True)
        return Response(serializer.data)


class CameraDetailView(RetrieveAPIView):
    queryset = Camera.objects.all()
    serializer_class = CameraDetailSerializer